<template>
	<!-- 修改车辆 -->
	<view class="addCar">
		<!-- <u-navbar title="修改车辆" title-color="#333" back-icon-color="#333"></u-navbar> -->
		<view class="addCar-bottom">
			<view class="addCar-content-box">
				<!--  -->
				<view class="addCar-carName-phone">
					<text class="addCar-phone">车牌号：</text>
					<!-- <input class="addCar-input-one" type="text" v-model="carNumberIpt"/> -->
					<view class="addCar-phone-car" @click="boardClick">
						{{carNumberIpt}}
						<text :class="focusIdex" :style="bgcolor"></text>
						<i class="addCar-phone-placeholder" v-if="carNumberIpt==''">请输入车牌号</i>
					</view>
				</view>

				<view class="addCar-carName-phone">
					<text class="addCar-phone">车主姓名：</text>
					<input class="addCar-input-one" type="text" placeholder="请输入司机姓名" v-model="nameIpt"
						maxlength="15" />
				</view>
				<view class="add-phone-main">
				<view class="addCar-carName-phone item-box" v-for="(item, index) in list">
					<text class="addCar-phone">联系方式：</text>
					<input class="addCar-input-one" type="number" @input="phoneIptInput" placeholder="请输入联系方式"
						v-model="item.phone" maxlength="11" />
					<span style="color: red;" v-if="list.length>=2" @click="delItem(item)">删除</span>
				</view>
				<view class="add-phone" @click="addItem">添加</view>
				</view>
				<view class="addCar-carName-phone">
					<text class="addCar-phone">皮重：</text>
					<input class="addCar-input-one" @input="tareIptInput" placeholder="请输入皮重" type="number"
						v-model="tareIpt" />
					<text class="addCar-dun">吨</text>
				</view>
			</view>
			<!--  -->
			<view class="addCar-reset-determine">
				<!-- <view class="add-phone" @click="addItem">新增电话</view> -->
				<view class="addCar-reset" @click="reset">重置</view>
				<view class="addCar-determine" @click="determine">确定</view>
			</view>
		</view>
		<u-modal v-model="addCarLoginShow" content="请确认是否修改该车辆" :show-cancel-button="true" @confirm="addCarConfirm">
		</u-modal>
		<u-keyboard ref="uKeyboard" mode="car" @change="carValChange" @backspace="carBackspace" v-model="carshow"
			:mask="false"></u-keyboard>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: JSON.parse(uni.getStorageSync('userinfo')),
				componeryArr: [],
				addressNew: null,
				Index: 0,
				diasabledInput: true, //可读不可写
				carNumberIpt: '', //车牌号
				carNumberIptMo: '', //判断未修改车牌号时防止提醒车牌号已存在
				nameIpt: '', //车主姓名
				phoneIpt: '', //联系方式
				tareIpt: '', //联系方式
				SETTB402ID: '',
				addCarLoginShow: false,
				carshow: false,
				id: 1,
				list: []

			}
		},
		onLoad(option) {
			console.log(option)
			let testArray
			testArray = JSON.parse(option.phoneIpt)//JSON.parse()转成数组
			this.list = testArray
			// if(typeof(option.phoneIpt) == 'object'){
			// 	console.log(1111111111)
				
				
			// }
			this.carNumberIpt = option.carNumberIpt
			this.carNumberIptMo = option.carNumberIpt
			this.nameIpt = option.nameIpt
			
			this.tareIpt = option.tareIpt
			this.SETTB402ID = option.SETTB402ID
			this.carID = option.carID
		},
		onShow() {

		},
		methods: {

			addItem() {
				if (this.list.length == 3) {
					uni.showToast({
						title: '最多三个',
						icon: 'none'
					})
					return false
				}
				// let add = {
				// 	id: parseInt(this.id) + 1
				// }
				this.list.push({
					phone: ''
				})
				// this.id = parseInt(this.id) + 1
				// this.phoneIpt += parseInt(this.id) + 1
				console.log(this.list)
			},
			delItem(item) {
				if (this.list.length == 1) {
					uni.showToast({
						title: '不能删除第1个',
						icon: 'none'
					})
					return false;
				}
				this.list = this.list.filter((i) => {
					return i != item
				})
			},
			submitBtn() {
				console.log(this.list)
			},

			carValChange(val) {
				// 将每次按键的值拼接到carNumberIpt变量中，注意+=写法
				this.carNumberIpt += val;
			},
			carBackspace() {
				// 删除carNumberIpt的最后一个字符
				if (this.carNumberIpt.length) this.carNumberIpt = this.carNumberIpt.substr(0, this.carNumberIpt.length -
					1);
			},
			boardClick() {
				this.carshow = true
			},
			tareIptInput() { //限制验证码长度
				if (this.tareIpt.length > 6) {
					this.tareIpt = this.tareIpt.slice(0, 6)
				}
			},
			phoneIptInput() {
				if (this.phoneIpt.length > 11) {
					this.phoneIpt = this.phoneIpt.slice(0, 11)
				}
			},
			// 确定
			determine() {
				//车牌号
				var regC =
					/^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领])|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][0-9]{2}([0-9]|[A-Z]){5}))$/;
				var regP = /^1[3-9][0-9]{9}$/;
				if (!regC.test(this.carNumberIpt)) return uni.showToast({
					title: '车牌号格式不正确，请检查',
					duration: 5000,
					icon: 'none',
				});
				if (this.nameIpt == '') {
					return uni.showToast({
						title: '请输入车主姓名',
						duration: 5000,
						icon: 'none',
					});
				}
				for(let index = 0; index < this.list.length; index++){
					if(this.list[index].phone ==''){
						return uni.showToast({
							title: '请输入手机号',
							duration: 5000,
							icon: 'none',
						});
					}
					if (this.list[index].phone != '') {
						if (!regP.test(this.list[index].phone)) return uni.showToast({
							title: '手机号格式不正确，请检查',
							duration: 5000,
							icon: 'none',
						});
					}
				}
				// if (this.phoneIpt == '') {
				// 	return uni.showToast({
				// 		title: '请输入手机号',
				// 		duration: 5000,
				// 		icon: 'none',
				// 	});
				// }
				// if (this.phoneIpt != '') {
				// 	console.log(this.phoneIpt)
				// 	if (!regP.test(this.phoneIpt)) return uni.showToast({
				// 		title: '手机号格式不正确，请检查',
				// 		duration: 5000,
				// 		icon: 'none',
				// 	});
				// }
				if (this.tareIpt == '') {
					return uni.showToast({
						title: '请输入皮重',
						duration: 5000,
						icon: 'none',
					});
				}
				this.addCarLoginShow = true

			},
			addCarConfirm() {
				if (this.userinfo.sign == 'CUS-1') {
					var ORMTB400ID = this.userinfo.ORMTB400ID
				} else if (this.userinfo.sign == 'CUS-2' || this.userinfo.sign == 'CUS-3') {
					var ORMTB400ID = this.userinfo.ORMTB410ID
				}
				console.log(this.list)
				
				uni.request({
					url: this.baseUrl + '/set/settb402/update',
					method: 'POST',
					dataType: 'json',
					data: {
						SETTB402CA00: this.carNumberIpt,
						revise: this.carNumberIptMo,
						SETTB402CA01: this.nameIpt,
						SETTB402CA02: this.list,
						SETTB402CA03: this.tareIpt,
						SETTB402ID: this.SETTB402ID,
						personID: ORMTB400ID,
						sign: this.userinfo.sign,
						carId: this.carID,
					},
					success: (res) => {
						console.log(res);
						if (res.data.code == 200) {
							if (res.data.message == '车牌号已存在,不能重复添加!') {
								uni.showToast({
									title: res.data.message,
									duration: 5000,
									icon: 'none',
								});
							} else {
								uni.showToast({
									title: '修改成功',
									duration: 5000,
									icon: 'none',
								});
								uni.navigateBack()
							}

						} else {
							uni.showToast({
								title: res.data.message,
								duration: 5000,
								icon: 'none',
							});
						}
					},
					fail: (res) => {
						uni.showToast({
							title: res.data.message,
							duration: 5000,
							icon: 'none',
						});
					}
				});
			},
			// 重置
			reset() {
				this.carNumberIpt = '' //车牌号
				this.nameIpt = '' //车主姓名
				this.phoneIpt = '' //联系方式
				this.tareIpt = ''
			},
		}
	}
</script>

<style scoped>
	.addCar input {
		border: 0;
	}

	.addCar {
		width: 100%;
		min-height: 100vh;
		background-color: rgba(242, 242, 242, 1);
		overflow: hidden;
	}

	/* 列表 */

	/* 内容 */
	.addCar-content-box {
		width: 100%;
		background: rgba(255, 255, 255, 1);
		padding: 0 24rpx;
	}

	.addCar-carName-phone {
		border-bottom: 2rpx solid rgba(230, 230, 230, 1);
		padding: 18rpx 0;
	}

	.addCar-input-one {
		width: 310rpx !important;
		display: inline-block;
		margin-bottom: 0 !important;
		padding: 0 !important;
		height: 44rpx !important;
		vertical-align: middle;
		/* margin-left: 0.1rem; */
		color: rgba(51, 51, 51, 1);
		font-size: 28rpx;
		text-align: center;
	}

	.addCar-phone {
		width: 241rpx;
		display: inline-block;
		font-size: 30rpx;
		color: rgba(102, 102, 102, 1);
		vertical-align: middle;
		/* background: #000000; */
	}

	.addCar-phone-placeholder {
		font-size: 30rpx;
		color: rgba(102, 102, 102, .5);
	}

	.addCar-dun {
		color: rgba(51, 51, 51, 1);
		font-size: 30rpx;
	}

	/* 按钮 */
	.addCar-reset-determine {
		margin-top: 167rpx;
		display: flex;
		justify-content: center;
	}

	.addCar-reset-determine>view {
		width: 159rpx;
		text-align: center;
		line-height: 65rpx;
		color: rgba(255, 255, 255, 1);
		font-size: 32rpx;
	}

	.addCar-reset {
		width: 159rpx;
		background: rgba(234, 197, 15, 1);
		text-align: center;
		/* margin-left: 170rpx; */
		margin: 0 20rpx;
	}

	.addCar-determine {
		background: rgba(0, 161, 244, 1);
		/* margin-left: 60rpx; */
	}

	.add-phone-main {
		position: relative;
	}
	
	.add-phone {
		position: absolute;
		top: 20rpx;
		right: 0;
		/* font-size: 50rpx; */
		color: rgba(0, 255, 127, 1);
		/* background: rgba(0, 255, 127, 1); */
	
	}

	.addCar-phone-car {
		display: inline-block;
		width: 310rpx;
		height: 44rpx;
		line-height: 44rpx;
		/* border: 1px solid rgba(0, 0, 0, .2); */
		/* border-radius: 3px; */
		text-align: center;
		vertical-align: middle;
		font-size: 30rpx;
	}

	input::placeholder {
		color: rgba(102, 102, 102, .5);
	}

	.item-box {
		display: flex;
	}
</style>
